Key to CSE142 Midterm, Spring 2012 1. Expression Value ----------------------------------------------- 1 / 2 * 5 + 12 12 7 % 4 + 17 % (14 % 10) 4 2 * 2.5 * 3 / 2 + 5 / 2 9.5 4 + 4 + "4 * 4" + 4 + 4 * 4 "84 * 4416" (6 > 5 && 4 < 3) || 3 > 2 true 2. Parameter Mystery. The program produces the following output. make fire from water and ether make water from ether and fire make ether from air and water make ether from fire and fire 3. Method Call Output Produced --------------------------------------- ifElseMystery(12, 45); 12 44 ifElseMystery(15, 5); 3 4 ifElseMystery(64, 8); 13 8 ifElseMystery(12, 12); 1 11 4. Method Call Output Produced -------------------------------------- mystery(2, 10); 10, 5, 1 mystery(2, 20); 20, 10, 3 mystery(3, 600); 600, 200, 50, 10 mystery(5, 100); 100, 20, 3, 0 5. c == 0 a < 0 a == b +---------------------+---------------------+---------------------+ Point A | always | sometimes | never | +---------------------+---------------------+---------------------+ Point B | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point C | sometimes | never | never | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ 6. Three possible solutions appear below: public static int commonDifference(int a, int b, int c) { if ((a + b) / 2.0 == c) return Math.abs(b - c); if ((a + c) / 2.0 == b) return Math.abs(a - b); if ((b + c) / 2.0 == a) return Math.abs(c - a); return -1; } public static int commonDifference3(int a, int b, int c) { int max = Math.max(a, Math.max(b, c)); double mid = (a + b + c) / 3.0; if (a == mid || b == mid || c == mid) { return max - (int) mid; } else { return -1; } } public static int commonDifference(int a, int b, int c) { if (a - b == b - c) { return Math.abs(a - b); } else if (b - c == c - a) { return Math.abs(b - c); } else if (c - a == a - b) { return Math.abs(c - a); } else { return -1; } } 7. One possible solution appears below: public static void range(Scanner console) { System.out.print("number? "); int val = console.nextInt(); int min = val; int max = val; while (val != 0) { min = Math.min(min, val); max = Math.max(max, val); System.out.print("number? "); val = console.nextInt(); } System.out.println("range: " + (max - min)); } 8. Three possible solutions appears below: public static int matchingRegions(String a, String b) { int matches = 0; int stop = Math.min(a.length(), b.length()); boolean inMatch = false; for (int i = 0; i < stop; i++) { if (!inMatch && a.charAt(i) == b.charAt(i)) { matches++; inMatch = true; } else if (a.charAt(i) != b.charAt(i)) { inMatch = false; } } return matches; } public static int matchingRegions(String a, String b) { int stop = Math.min(a.length(), b.length()); int matches = 0; boolean inMatch = false; for (int i = 0; i < stop; i++) { if (a.charAt(i) != b.charAt(i)) { inMatch = false; } else if (!inMatch) { inMatch = true; matches++; } } return matches; } public static int matchingRegions(String a, String b) { int stop = Math.min(a.length(), b.length()); int matches = 0; int start = 0; for (int i = 0; i < stop; i++) { if (a.charAt(i) == b.charAt(i)) { matches++; start = i; while (i < stop && a.substring(start, i + 1).equals(b.substring(start, i + 1))) { i++; } } } return matches; }